package com.jollychic.www.roomdatebasedemo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jollychic.www.roomdatebasedemo.DaoManager;
import com.jollychic.www.roomdatebasedemo.R;
import com.jollychic.www.roomdatebasedemo.model.ComputerEntity;
import com.jollychic.www.roomdatebasedemo.model.DrinksEntity;
import com.jollychic.www.roomdatebasedemo.model.GoodEntity;
import com.jollychic.www.roomdatebasedemo.model.StoreEntity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Completable;
import io.reactivex.schedulers.Schedulers;

/**
 * desc: 增加或重置数据 <br/>
 * author: 李超杰 <br/>
 * time: 2018/4/22 17:42 <br/>
 * since: V 6.1X <br/>
 */

public class FragmentCreate extends Fragment {

    private AppCompatButton btCreate;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = LayoutInflater.from(getContext())
                .inflate(R.layout.fragment_create, container, false);
        btCreate = view.findViewById(R.id.bt_create);
        loadListener();
        return view;
    }

    private void loadListener() {
        btCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadData();
            }
        });
    }

    /**
     * 加载数据
     */
    private void loadData() {
        Completable
                .fromRunnable(new Runnable() {
                    @Override
                    public void run() {
                        //店铺
                        final List<StoreEntity> storesList = DaoManager.getInstance()
                                .getIStoreDao()
                                .getStoresList();

                        //商品
                        final List<GoodEntity> goodEntities = DaoManager.getInstance()
                                .getIGoodsDao()
                                .queryGoodsList();

                        //drinks
                        final List<DrinksEntity> drinksEntities = DaoManager.getInstance()
                                .getIDrinkDao()
                                .queryDrinks();

                        if (storesList != null && !storesList.isEmpty()) {
                            DaoManager.getInstance().getIStoreDao().delete(storesList);
                        }

                        if (goodEntities != null && !goodEntities.isEmpty()) {
                            DaoManager.getInstance().getIGoodsDao().deleteGoods(goodEntities);
                        }

                        final List<StoreEntity> list = new ArrayList<>();
                        final List<GoodEntity> goodEntityList = new ArrayList<>();

                        //1.模拟加载店铺数据
                        for (int i = 0; i < 30; i++) {
                            final StoreEntity storeEntity = new StoreEntity();
                            storeEntity.setStoreId(String.valueOf(i));
                            storeEntity.setStoreName(String.format("店铺%s", i));

                            if (i == 0) {
                                //当店铺id为1 模拟加载条Computer数据
                                final ComputerEntity computerEntity =
                                        new ComputerEntity("store0_computer1", 3500.0, "DELL");
                                storeEntity.setComputerEntity(computerEntity);
                            }

                            if (i == 1 || i == 3) {
                                // TODO: 2018/5/9 当店铺id为1的时候 模拟加载一组List<ComputerEntity> list
                                final List<ComputerEntity> computerEntities = new ArrayList<>();
                                for (int j = 0; j < 10; j++) {
                                    final ComputerEntity computerEntity = new ComputerEntity();
                                    computerEntity.setName(String.format("store1_computer_name_%s", j));
                                    computerEntity.setType(String.format("store1_computer_type_%s", j));
                                    computerEntity.setPrice(Double.valueOf((j + 1) * 900));
                                    computerEntities.add(computerEntity);
                                }
                                storeEntity.setComputerEntityList(computerEntities);
                            }

                            if (i == 20) {
                                //当店铺id为20时 模拟添加几条 id不同 店铺名称不同的数据  主要用来限数查找 limit
                                for (int j = 2; j < 10; j++) {
                                    final StoreEntity storeEntityTemp = new StoreEntity();
                                    storeEntityTemp.setStoreId(String.valueOf(j * 20));
                                    storeEntityTemp.setStoreName(String.format("店铺%s", i));
                                    list.add(storeEntityTemp);
                                }
                            }
                            list.add(storeEntity);
                        }

                        //2.模拟添加商品 -- 店铺对应店铺id为0
                        for (int j = 0; j < 3; j++) {
                            //为了区别店铺id 把商品的id 定义为 店铺id_S_商品id
                            final GoodEntity goodEntity = new GoodEntity();
                            goodEntity.setGoodId(String.format("0_S%s", j));
                            goodEntity.setName(String.format("店铺0_衣服款式%s", j));
                            //设置需要对应的storeId
                            goodEntity.setStoreId(String.valueOf(j * 3));
                            goodEntityList.add(goodEntity);
                        }

                        //3.模拟加载drinks中的数据 用于多表查询
                        if (drinksEntities.isEmpty()) {
                            final List<DrinksEntity> drinksEntityList = new ArrayList<>();

                             drinksEntityList.add(new DrinksEntity(2.5,"MD",String.valueOf(3)));
                             drinksEntityList.add(new DrinksEntity(4.0,"WH",String.valueOf(5)));
                             drinksEntityList.add(new DrinksEntity(7.0,"OG",String.valueOf(10)));
                             drinksEntityList.add(new DrinksEntity(3.6,"KL",String.valueOf(15)));
                             drinksEntityList.add(new DrinksEntity(9.5,"SR",String.valueOf(21)));
                            // TODO: 2018/5/20
//                            DaoManager.getInstance().getIDrinkDao().insert(drinksEntityList);
                        }

                        //把模拟加载的数据  加入数据库
                        DaoManager.getInstance().getIStoreDao().insert(list);
                        DaoManager.getInstance().getIGoodsDao().insertGoods(goodEntityList);
                    }
                })
                .subscribeOn(Schedulers.io())
                .subscribe();
    }
}
